<?php

class LogStateAction extends EditAction {

	public $logModelName;
	public $nextState;
	public $stateHandle = 'stateHandle';
	public $detailView = array();
	
	public function run($id) {
		$c = $this->controller;
		$model = $c->loadModel($id);
		
		$logModel = new $this->logModelName;
		$this->_model = $logModel;
		
		if (isset($_POST[$this->logModelName])) {
			$logModel->attributes = $_POST[$this->logModelName];
			if ($logModel->save() && $model->{$this->stateHandle}->setState($this->nextState)) {
				echo 1;
				Yii::app()->end();
			}
			if($model->getErrors()) {
				Yii::app()->user->setFlash(FLASH::RESULT_FAILED, CHtml::errorSummary($model));
			}
		}
		
		$c->renderPartial($this->view,array(	
			'model' => $model,
			'logModel' => $logModel,
			'formView' => array_merge($this->getFormView(),array('model'=>$logModel)),
			'detailView' => $this->config($model),
		));
	}
	
	public function config($model) {
		$config = $this->detailView;
		$config['data'] = $model;
		
		
		if(!isset($config['attributes'])) {
			foreach($model->attributeNames() as $one) {
				$config['attributes'][] = $one;
			}
		}
		return $config;
	}
	
}

?>